package com.support.android.myapp3.util;

import java.math.BigDecimal;

public class Money {	
	public static BigDecimal ONE_HUNDRED = new BigDecimal("100.00000");
	private BigDecimal _yuan = new BigDecimal("0.00000");

	private long cacheFen = 0;

	public void cacheFen(){
	    this.cacheFen = getFen();
    }

    //缓存数据，只有调用cacheFen()方法后才会更新缓存数据,一定小心使用
    public long getCacheFen(){
	    return cacheFen;
    }

	public  Money setYuan(String yuanStr){
		_yuan = new BigDecimal(yuanStr);
		return this;
	}

    public  Money setYuan(BigDecimal yuanDecimal){
        _yuan = yuanDecimal;
        return this;
    }

	public static  Money instanceYuan(String yuan){
		Money m = new Money();
		m.setYuan(yuan);
		return m;
	}

    public static  Money instanceYuan(BigDecimal yuan){
        Money m = new Money();
        m.setYuan(yuan);
        return m;
    }

	public static Money instanceFen(String fen){
		Money m = new Money();
		m.setFen(fen);
		return m;
	}
	
	public Money setFen(String fenStr){
		BigDecimal fenBigDecimal = new BigDecimal(fenStr);
		_yuan = fenBigDecimal.divide(ONE_HUNDRED);
		return this;
	}
	
	public String getYuan(){
		return getYuan(2);
	}
	
	public String getYuan(int scale){
		return _yuan.setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
	}

    public String getYuan(int scale,int roundingMode){
        return _yuan.setScale(scale, roundingMode).toString();
    }

    public String getYuan_Round_UP(){
	    return getYuan(2, BigDecimal.ROUND_UP);
    }

	public BigDecimal getYuanBigDecimal(){
	    return _yuan;
    }
	
	public long getFen(){
		return _yuan.multiply(ONE_HUNDRED).setScale(0,BigDecimal.ROUND_HALF_UP).longValue();
	}
	// per  21%  write 21
	public Money multiplyPercentage(String per){
        return Money.instanceYuan( _yuan.multiply(new BigDecimal(per).divide(ONE_HUNDRED,5, BigDecimal.ROUND_HALF_UP)));
    }

    public Money multiplyNum(String num){
        return Money.instanceYuan(_yuan.multiply(new BigDecimal(num)));
    }

    public Money divideNum(String num){
        return Money.instanceYuan(_yuan.divide(new BigDecimal(num),5, BigDecimal.ROUND_HALF_UP));
    }

    public Money divideMoney(Money dMoney){
	    if(dMoney.getFen() == 0){
	        throw new RuntimeException("被除数不能为0");
        }
        return Money.instanceYuan(_yuan.divide(dMoney.getYuanBigDecimal(),5, BigDecimal.ROUND_HALF_UP));
    }

    public Money divideYuan(String yuanStr){
        Money dd = Money.instanceYuan(yuanStr);
        if(dd.getFen() == 0){
            throw new RuntimeException("被除数不能为0");
        }

        return Money.instanceYuan(_yuan.divide(dd.getYuanBigDecimal(),5, BigDecimal.ROUND_HALF_UP));
    }

    public Money divideFen(String fenStr){
        Money dd = Money.instanceFen(fenStr);
        if(dd.getFen() == 0){
            throw new RuntimeException("被除数不能为0");
        }

        return Money.instanceYuan(_yuan.divide(dd.getYuanBigDecimal(),5, BigDecimal.ROUND_HALF_UP));
    }

    public Money addMoney(Money aMoney){
        return Money.instanceYuan(_yuan.add(aMoney.getYuanBigDecimal()));
    }

    public Money addMoney(Money... aMoney){
       for(Money money:aMoney){
           _yuan = addMoney(money).getYuanBigDecimal();
       }
        return this;
    }

	public Money addYuan(String yuanStr){
        return Money.instanceYuan(_yuan.add(new BigDecimal(yuanStr)));
	}

    public Money subtractMoney(Money sMoney){
        return Money.instanceYuan( _yuan.subtract(sMoney.getYuanBigDecimal()));
    }

    public Money subtractMoney(Money... aMoney){
        for(Money money:aMoney){
              _yuan = subtractMoney(money).getYuanBigDecimal();
        }
        return this;
    }

	public Money subtractYuan(String yuanStr){
        return Money.instanceYuan(_yuan.subtract(new BigDecimal(yuanStr)));
	}
	
	public Money addFen(String fenStr){
		BigDecimal fenBigDecimal = new BigDecimal(fenStr);
        return Money.instanceYuan(_yuan.add(fenBigDecimal.divide(ONE_HUNDRED)));
	}
	
	public Money subtractFen(String fenStr){
		BigDecimal fenBigDecimal = new BigDecimal(fenStr);
        return Money.instanceYuan(_yuan.subtract(fenBigDecimal.divide(ONE_HUNDRED)));
	}

	public static long yuanToFen(String yuan){
		BigDecimal yuanBigDecimal = new BigDecimal(yuan);
		return yuanBigDecimal.multiply(ONE_HUNDRED).setScale(0,BigDecimal.ROUND_HALF_UP).longValue();
	}
	
	public static String fenToYuan(String fen,int scale){
		BigDecimal fenBigDecimal = new BigDecimal(fen);
		return fenBigDecimal.divide(ONE_HUNDRED, scale, BigDecimal.ROUND_HALF_UP).toString();
	}
	
	public static String fenToYuan(String fen){
		return fenToYuan(fen,2);
	}
}
